package com.shilox.android.app.logitnow.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.shilox.android.app.logitnow.R;
import com.shilox.android.app.logitnow.dao.LogMessageDAO;

public class AddFeelingFragment extends Fragment implements OnClickListener {
	private static final int VERY_SAD_COLOR = Color.argb(255, 163, 139, 138);
	private static final int SAD_COLOR = Color.argb(255, 201, 211, 214);
	private static final int NORMAL_COLOR = Color.argb(255, 191, 230, 164);
	private static final int HAPPY_COLOR = Color.argb(255, 230, 140, 141);
	private static final int VERY_HAPPY_COLOR = Color.argb(255, 255, 54, 70);

	// GUI components
	private EditText logMessageText; // Text field
	// private Button addNewButton; // Add new button
	private ImageView verySadButton;
	private ImageView sadButton;
	private ImageView normalButton;
	private ImageView happyButton;
	private ImageView veryHappyButton;
	private LinearLayout verySadLayout;
	private LinearLayout sadLayout;
	private LinearLayout normalLayout;
	private LinearLayout happyLayout;
	private LinearLayout veryHappyLayout;
	private LinearLayout addLayout;

	// DAO
	private LogMessageDAO dao;

	private Integer lastPressed;
	private Integer defaultFace = 3;

	public static final AddFeelingFragment newInstance(String message) {
		AddFeelingFragment f = new AddFeelingFragment();
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.activity_add_logmessage, container,
				false);

		lastPressed = defaultFace;
		// Create DAO object
		dao = new LogMessageDAO(getActivity());

		logMessageText = (EditText) v.findViewById(R.id.logMessageText);
		// addNewButton = (Button) v.findViewById(R.id.addNewLogMessageButton);

		verySadButton = (ImageView) v.findViewById(R.id.verySadButton);
		sadButton = (ImageView) v.findViewById(R.id.sadButton);
		normalButton = (ImageView) v.findViewById(R.id.normalButton);
		happyButton = (ImageView) v.findViewById(R.id.happyButton);
		veryHappyButton = (ImageView) v.findViewById(R.id.veryHappyButton);
		verySadLayout = (LinearLayout) v.findViewById(R.id.verySadLayout);
		sadLayout = (LinearLayout) v.findViewById(R.id.sadLayout);
		normalLayout = (LinearLayout) v.findViewById(R.id.normalLayout);
		happyLayout = (LinearLayout) v.findViewById(R.id.happyLayout);
		veryHappyLayout = (LinearLayout) v.findViewById(R.id.veryHappyLayout);
		addLayout = (LinearLayout) v.findViewById(R.id.addLayout);

		verySadLayout.setBackgroundColor(VERY_SAD_COLOR);
		sadLayout.setBackgroundColor(SAD_COLOR);
		normalLayout.setBackgroundColor(NORMAL_COLOR);
		happyLayout.setBackgroundColor(HAPPY_COLOR);
		veryHappyLayout.setBackgroundColor(VERY_HAPPY_COLOR);

		// addNewButton.setOnClickListener(this);
		verySadButton.setOnClickListener(this);
		sadButton.setOnClickListener(this);
		normalButton.setOnClickListener(this);
		happyButton.setOnClickListener(this);
		veryHappyButton.setOnClickListener(this);
		// days = (Integer) getIntent().getExtras().get("days");
		addLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (lastPressed == 0) {
					Toast.makeText(getActivity().getApplicationContext(),
							"Select feeling!", Toast.LENGTH_LONG).show();
					return;
				}
				// Get entered text
				String logMessageTextValue = logMessageText.getText()
						.toString().trim();

				if (logMessageText.getText() == null
						|| logMessageTextValue.length() == 0) {
					Toast.makeText(getActivity().getApplicationContext(),
							"Write something!", Toast.LENGTH_LONG).show();
					return;
				}

				// Add text to the database
				dao.createLogMessage(logMessageTextValue, lastPressed);

				reset();

				// Display success information
				Toast.makeText(getActivity().getApplicationContext(),
						"New feeling!", Toast.LENGTH_LONG).show();
			}
		});

		// frame.setBackgroundColor(NORMAL_COLOR);
		addLayout.setBackgroundColor(NORMAL_COLOR);
		return v;
	}

	public void onClickAddLayout(View v) {
		if (lastPressed == 0) {
			Toast.makeText(getActivity().getApplicationContext(),
					"Select feeling!", Toast.LENGTH_LONG).show();
			return;
		}
		// Get entered text
		String logMessageTextValue = logMessageText.getText().toString().trim();

		if (logMessageText.getText() == null
				|| logMessageTextValue.length() == 0) {
			Toast.makeText(getActivity().getApplicationContext(),
					"Write something!", Toast.LENGTH_LONG).show();
			return;
		}

		// Add text to the database
		dao.createLogMessage(logMessageTextValue, lastPressed);

		reset();

		// Display success information
		Toast.makeText(getActivity().getApplicationContext(), "New feeling!",
				Toast.LENGTH_LONG).show();
	}

	private void reset() {
		logMessageText.setText("");
		lastPressed = defaultFace;
		// frame.setBackgroundColor(NORMAL_COLOR);
		addLayout.setBackgroundColor(NORMAL_COLOR);
		faceOff();
		faceOn(defaultFace);
	}

	@Override
	public void onClick(View v) {
		if (veryHappyButton.isPressed() || verySadButton.isPressed()
				|| happyButton.isPressed() || sadButton.isPressed()
				|| normalButton.isPressed()) {
			Integer pressed = 0;
			// Averiguo la pulsada
			if (verySadButton.isPressed()) {
				pressed = 1;
			} else if (sadButton.isPressed()) {
				pressed = 2;
			} else if (normalButton.isPressed()) {
				pressed = 3;
			} else if (happyButton.isPressed()) {
				pressed = 4;
			} else if (veryHappyButton.isPressed()) {
				pressed = 5;
			} else {
				Toast.makeText(getActivity().getApplicationContext(), "Error!",
						Toast.LENGTH_LONG).show();
				return;
			}
			faceOff();
			lastPressed = pressed;
			faceOn(pressed);
		}
	}

	private void faceOff() {
		verySadButton.setImageDrawable(getResources().getDrawable(
				R.drawable.verysad_off));
		sadButton.setImageDrawable(getResources().getDrawable(
				R.drawable.sad_off));
		normalButton.setImageDrawable(getResources().getDrawable(
				R.drawable.normal_off));
		happyButton.setImageDrawable(getResources().getDrawable(
				R.drawable.happy_off));
		veryHappyButton.setImageDrawable(getResources().getDrawable(
				R.drawable.veryhappy_off));

		verySadButton.invalidate();
		sadButton.invalidate();
		normalButton.invalidate();
		happyButton.invalidate();
		veryHappyButton.invalidate();
	}

	private void faceOn(Integer index) {
		switch (index) {
		case 1:
			addLayout.setBackgroundColor(VERY_SAD_COLOR);
			verySadButton.setImageDrawable(getResources().getDrawable(
					R.drawable.verysad_on));
			verySadButton.invalidate();
			break;

		case 2:
			addLayout.setBackgroundColor(SAD_COLOR);
			sadButton.setImageDrawable(getResources().getDrawable(
					R.drawable.sad_on));
			sadButton.invalidate();
			break;
		case 3:
			addLayout.setBackgroundColor(NORMAL_COLOR);
			normalButton.setImageDrawable(getResources().getDrawable(
					R.drawable.normal_on));
			normalButton.invalidate();
			break;
		case 4:
			addLayout.setBackgroundColor(HAPPY_COLOR);
			happyButton.setImageDrawable(getResources().getDrawable(
					R.drawable.happy_on));
			happyButton.invalidate();
			break;
		case 5:
			addLayout.setBackgroundColor(VERY_HAPPY_COLOR);
			veryHappyButton.setImageDrawable(getResources().getDrawable(
					R.drawable.veryhappy_on));
			veryHappyButton.invalidate();
			break;
		}
	}

}
